﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeDuce.Model;
using System.IO;
using SeDuce.Utility;
using SeDuce.BLL;

public partial class Management_Products_PicUpload : PageBase
{
    List<Pic> pics = new List<Pic>();
    Lazy_Yu<PicBLL> lazyPicBLL = new Lazy_Yu<PicBLL>(() => new PicBLL());
    public PicBLL LazyPicBLL
    {
        get { return this.lazyPicBLL.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lbtnSave_Click(object sender, EventArgs e)
    {
        if (Session["file_info"] != null)
        {
            List<string> file_list_to_delete = new List<string>();
            try
            {
                List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;

                string UploadPath = Server.MapPath(Misc.product_filepath + "/" + this.GetQueryString("ProductID", false));
                if (!System.IO.Directory.Exists(UploadPath))
                {
                    System.IO.Directory.CreateDirectory(UploadPath);
                }

                foreach (Thumbnail img in thumbnails)
                {
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString().Replace("-", "");
                    string fullfilename = filename + Path.GetExtension(img.ID).ToLower();
                    file_list_to_delete.Add(Misc.product_filepath + "/" + this.GetQueryString("ProductID", false) + "/" + fullfilename);
                    file_list_to_delete.Add(Misc.product_filepath + "/" + this.GetQueryString("ProductID", false) + "/Temp_" + fullfilename);
                    file_list_to_delete.Add(Misc.product_filepath + "/" + this.GetQueryString("ProductID", false) + "/Temp_Watermark_" + fullfilename);
                    FileStream fs = new FileStream(UploadPath + "\\Temp_" + fullfilename, FileMode.Create);
                    BinaryWriter bw = new BinaryWriter(fs);
                    bw.Write(img.Data);
                    bw.Close();
                    fs.Close();

                    //watermark
                    Utils.Watermark(UploadPath + "\\Temp_" + fullfilename, UploadPath + "\\Temp_Watermark_" + fullfilename, Server.MapPath(Misc.watermark2), "CENTER");
                    Utils.Watermark(UploadPath + "\\Temp_Watermark_" + fullfilename, UploadPath + "\\" + fullfilename, Server.MapPath(Misc.watermark1), "BR");

                    pics.Add(new Pic()
                    {
                        PicName = img.ID,
                        Url = Misc.product_filepath + "/" + this.GetQueryString("ProductID", false) + "/" + fullfilename,
                        ProductID = this.GetQueryString("ProductID", false).ToInt(),
                        Sort = 0
                    });
                }
                if (this.LazyPicBLL.InsertPics(pics))
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key1", "javascript:" + string.Format("alert('{0}');window.location.href='PicList.aspx?ProductID={1}';", "操作成功!", this.GetQueryString("ProductID", false)), true);
                }
                else
                {
                    throw new Exception("保存时发生错误");
                }
            }
            catch (Exception ex)
            {
                foreach (string file in file_list_to_delete)
                {
                    if (File.Exists(Server.MapPath(file)))
                    {
                        File.Delete(Server.MapPath(file));
                    }
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Key2", "javascript:" + string.Format("alert('{0}');window.location.reload();", "保存时发生错误!请重试..."), true);
            }
            finally
            {
                Session.Remove("file_info");
            }
        }
    }
    protected void lbtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("PicList.aspx?ProductID=" + this.GetQueryString("ProductID", false));
    }
}